package com.itggm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itggm.mapper.SlideMapper;
import com.itggm.pojo.Slide;
import com.itggm.response.ResponseResult;
import com.itggm.service.SlideService;
import com.itggm.utils.AliOSSUtil;
import com.itggm.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: ggm
 * @data 2024年11月20日 11:37
 */
@Service
public class SlideServiceImpl implements SlideService {
    @Autowired
    private SlideMapper slideMapper;
    @Autowired
    private AliOSSUtil aliOSSUtil;

    @Override
    public ResponseResult page(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Slide> slides = slideMapper.page();
        PageInfo<Slide> slidePageInfo = new PageInfo<>(slides);
        long total = slidePageInfo.getTotal();
        return ResponseResult.success().count(total).data(slides);
    }

    @Override
    public ResponseResult uploadInfo(Slide slide) {
        slideMapper.uploadInfo(slide);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult delete(String uuid) {
        String aliUrl = StringUtil.extractFileNameFromUrl(uuid);
        slideMapper.delete(aliUrl);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult update(Integer carouseId) {
        slideMapper.update(carouseId);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult show(Integer limit) {
        List<Slide> slides = slideMapper.selectPictureList(limit);
        return ResponseResult.success().data(slides);
    }

    @Override
    public ResponseResult uploadImg(Integer carouseId, String fileName, String uuid) {
        String url = StringUtil.extractFileNameFromUrl(uuid);
        try {
            aliOSSUtil.delete(url);
            slideMapper.uploadImg(carouseId, fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.success().message("修改成功");

    }


}
